package com.andy.wan.android.mvp.ui.fragment;

import android.content.Context;
import android.view.View;

import com.andy.ui.base.BaseListFragment;
import com.andy.ui.widget.PageStateView;
import com.andy.wan.android.R;
import com.andy.wan.android.mvp.api.WanAndroidApi;
import com.andy.wan.android.mvp.bean.BaseResponse;
import com.andy.wan.android.mvp.bean.WxAuthor;
import com.andy.wan.android.mvp.net.RetrofitUtil;
import com.andy.wan.android.mvp.ui.adapter.WxArticleAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import io.reactivex.Observable;

/**
 * @Author: luofan
 * @CreateDate: 2020/4/1 16:44
 * @Description:
 */
public class WxArticleFragment extends BaseListFragment<BaseResponse<List<WxAuthor>>, WxAuthor> {

    @BindView(R.id.recycler_view)
    RecyclerView mRecyclerView;
    @BindView(R.id.page_stat_view)
    PageStateView mPageStatView;

    @NonNull
    @Override
    protected Observable<BaseResponse<List<WxAuthor>>> getObservable() {
        return RetrofitUtil.getInstance().create(WanAndroidApi.class).getWxAuthorListData();
    }

    @NonNull
    @Override
    protected List<WxAuthor> getRecyclerDataFromResult(BaseResponse<List<WxAuthor>> response) {
        return response.getData();
    }

    @NonNull
    @Override
    protected RecyclerView.LayoutManager getLayoutManager(Context context) {
        return new GridLayoutManager(context, 2);
    }

    @Override
    protected RecyclerView.ItemDecoration getItemDecoration() {
        return null;
    }

    @NonNull
    @Override
    protected RecyclerView getRecyclerView() {
        return mRecyclerView;
    }

    @Nullable
    @Override
    protected SmartRefreshLayout getRefreshLayout() {
        return null;
    }

    @NonNull
    @Override
    protected BaseQuickAdapter<WxAuthor, BaseViewHolder> getAdapter() {
        WxArticleAdapter wxArticleAdapter = new WxArticleAdapter();
        wxArticleAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                WxAuthor wxAuthor = wxArticleAdapter.getItem(position);
                if (wxAuthor != null) {
                    FeedArticleListFragment.startMe(getContext(), wxAuthor.getName(), FeedArticleListFragment.TYPE_WX_ARTICLE, wxAuthor.getId());
                }
            }
        });
        return wxArticleAdapter;
    }

    @Override
    protected PageStateView getPageStateView() {
        return mPageStatView;
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_wx_article;
    }
}
